package com.taotao.web.service;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.taotao.common.service.HttpClientService;
import com.taotao.common.service.RedisService;
import com.taotao.manage.pojo.ItemDesc;

@Service
public class ItemDescService {

	@Autowired
	private HttpClientService httpClientService;

	@Autowired
	private RedisService redisService;

	@Value("${TATAO_MANAGE_ITEM_DESC}")
	private String TATAO_MANAGE_ITEM_DESC;

	@Value("${TAOTAO_WEB_ITEM_DESC_CACHE_PREFIX}")
	private String TAOTAO_WEB_ITEM_DESC_CACHE_PREFIX;

	@Value("${TAOTAO_WEB_ITEM_CACHE_TIMEOUT}")
	private Long TAOTAO_WEB_ITEM_CACHE_TIMEOUT;

	/**
	 * 根据商品id获取商品描述信息
	 * 
	 * @param itemId
	 * @return
	 */
	public ItemDesc queryItemDescById(Long itemId) {

		// 查询缓存
		try {
			ItemDesc itemDesc = this.redisService.get(TAOTAO_WEB_ITEM_DESC_CACHE_PREFIX + itemId,
					new TypeReference<ItemDesc>() {
					});
			if (itemDesc != null) {
				return itemDesc;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		ItemDesc itemDesc = this.httpClientService.doGet(TATAO_MANAGE_ITEM_DESC + itemId,
				new TypeReference<ItemDesc>() {
				});

		// 添加缓存
		try {
			this.redisService.set(TAOTAO_WEB_ITEM_DESC_CACHE_PREFIX + itemId, itemDesc, TAOTAO_WEB_ITEM_CACHE_TIMEOUT);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return itemDesc;
	}

}
